package com.zyg.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyg.page.entity.GoodsDescEntity;
import com.zyg.page.entity.GoodsEntity;
import com.zyg.page.entity.ItemEntity;
import com.zyg.page.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ------------------------------
 * 作者: LMY
 * 类-功能: XX
 * 创建时间: 2021-11-17 19:34
 * ------------------------------
 **/
@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private TemplateEngine templateEngine;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsDescService goodsDescService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private ItemCatService itemCatService;

    /**
     * 作者 : LMY
     * 功能 : 生成一给SKU商品的详情表的静态页面
     * 时间 : 2021/11/17 19:34
     * D:\app\nginx-1.18.0\nginx-1.18.0\html\item
     */
    @Override
    public void createHtml(Long id) {
        try {
            //1. 定义模板工作的上下文环境
            Context context = new Context();

            //2. 定义最终存放静态文件的位置
            File file = new File("C:\\Users\\Administrator\\Desktop\\static/" + id + ".html");

            //3. 实例输出流对象
            Writer writer = new FileWriter(file);

            //4. 定义存放数据的集合
            Map<String,Object> dataMap = new HashMap();

            //5. 根据id查询出SPU商品
            GoodsEntity goods = goodsService.getById(id);

            //6. 查询出对应的商品描述
            GoodsDescEntity goodsDesc = goodsDescService.getById(id);

            //7. 根据goods id查询出SKU商品列表，并根据默认状态排序
            List<ItemEntity> items = itemService.list(new QueryWrapper<ItemEntity>().eq("goods_id", id).orderByDesc("is_default"));

            //8. 将SPU商品放入集合
            dataMap.put("goods",goods);
            dataMap.put("goodsDesc",goodsDesc);

            //9 将第一个SKU商品，与SKU商品列表放入集合放入集合
            dataMap.put("items",items);
            dataMap.put("item",items.get(0));


            //10 查询各个spu商品的属性
            List<Map> customAttributeItems = JSON.parseArray(goodsDesc.getCustomAttributeItems(), Map.class);
            List<Map> specificationItems = JSON.parseArray(goodsDesc.getSpecificationItems(), Map.class);
            List<Map> itemImages = JSON.parseArray(goodsDesc.getItemImages(), Map.class);

            //11 查询分类
            String name1 = itemCatService.getById(goods.getCategory1Id()).getName();
            String name2 = itemCatService.getById(goods.getCategory2Id()).getName();
            String name3 = itemCatService.getById(goods.getCategory3Id()).getName();

            dataMap.put("customAttributeItems", customAttributeItems);
            dataMap.put("specificationItems",specificationItems);
            dataMap.put("itemImages",itemImages);
            dataMap.put("name1",name1);
            dataMap.put("name2",name2);
            dataMap.put("name3",name3);
            context.setVariables(dataMap);

            //生成静态页面
            templateEngine.process("item",context,writer);

            System.out.println("生成静态页面成功！ 文件名为：" + id + ".html" );

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
